package com.ruin.tech.ahri.service.impl;

import com.ruin.tech.ahri.dao.TmpArticleImgMapper;
import com.ruin.tech.ahri.dao.UserMapper;
import com.ruin.tech.ahri.dao.VideoMapper;
import com.ruin.tech.ahri.model.dto.*;
import com.ruin.tech.ahri.service.IUploadService;
import com.ruin.tech.ahri.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program ahri
 * @description: 上传服务
 * @author: ruin
 * @create: 2019/11/28 10:58
 */

@Service
public class UploadServiceImpl implements IUploadService {

    @Value("${upload.path.avatar}")
    private String uploadAvatarPath;

    @Value("${upload.path.video}")
    private String uploadVideoPath;

    @Value("${upload.path.articleImg}")
    private String uploadArticleImgPath;

    @Value("${upload.path.coverImg}")
    private String uploadCoverImgPath;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private TmpArticleImgMapper tmpArticleImgMapper;

    @Override
    public String saveAvatar(MultipartFile file, Long id) throws Exception {

        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid+"_"+fileName;
        File dFile = new File("./"+uploadAvatarPath);
        File pFile = new File("./"+uploadAvatarPath,newFileName);
        if(!dFile.exists()){
            dFile.mkdirs();
        }
        Utils.wirteFile(file,pFile);

        String avatarUrl = uploadAvatarPath+newFileName;

        UserExample example = new UserExample();
        example.createCriteria().andIdEqualTo(id);
        List<User> users = userMapper.selectByExample(example);
        User user = users.get(0);
        String headerPath = null;
        if(null != user){
            headerPath = user.getHeaderPath();
        }else{
            throw new Exception();
        }
        if(null != headerPath && !"".equals(headerPath)){
            File oldFile = new File("./"+headerPath);
            if(oldFile.exists()){
                oldFile.delete();
            }
        }
        user.setHeaderPath(avatarUrl);
        userMapper.updateByExampleSelective(user,example);
        return avatarUrl;
    }

    @Override
    public String saveVideo(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid+"_"+fileName;
        File dFile = new File("./"+uploadVideoPath);
        File pFile = new File("./"+uploadVideoPath,newFileName);
        if(!dFile.exists()){
            dFile.mkdirs();
        }
        Utils.wirteFile(file,pFile);

        String videoUrl = uploadVideoPath+newFileName;
        List<Video> videos = videoMapper.selectByExample(new VideoExample());
        if(videos.size()<1){
            Video video = new Video();
            video.setName("首页视频");
            video.setVideoPath(videoUrl);
            video.setCreateTime(new Date());
            videoMapper.insert(video);
        }else{
            Video video = videos.get(0);
            String oldVideoUrl = video.getVideoPath();
            if(null != oldVideoUrl && !"".equals(oldVideoUrl)){
                File oldFile = new File("./"+oldVideoUrl);
                if(oldFile.exists()){
                    oldFile.delete();
                }
            }
            VideoExample example = new VideoExample();
            example.createCriteria().andIdEqualTo(video.getId());
            video.setVideoPath(videoUrl);
            videoMapper.updateByExampleSelective(video,example);
        }

        return videoUrl;
    }

    @Override
    public String saveArticleImg(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid+"_"+fileName;
        File dFile = new File("./"+uploadArticleImgPath);
        File pFile = new File("./"+uploadArticleImgPath,newFileName);
        if(!dFile.exists()){
            dFile.mkdirs();
        }
        Utils.wirteFile(file,pFile);

        String articleImgUrl = uploadArticleImgPath+newFileName;

        TmpArticleImg tmpArticleImg = new TmpArticleImg();
        tmpArticleImg.setCreateTime(new Date());
        tmpArticleImg.setPath(articleImgUrl);
        tmpArticleImgMapper.insert(tmpArticleImg);
        return articleImgUrl;
    }

}
